package com.hzhq.contract.service.impl;

import com.hzhq.contract.bean.dto.AdminDTO;
import com.hzhq.contract.bean.entity.Admin;
import com.hzhq.contract.convert.AdminConvertBasic;
import com.hzhq.contract.repo.AdminRepository;
import com.hzhq.contract.service.AdminService;
import com.hzhq.contract.status.DefaultValue;
import com.hzhq.contract.util.Page2ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author: hzhq1255
 * @mail: hzhq1255@163.com
 * @date: 2021/4/14 21:05
 * @desc:
 */
@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminRepository adminRepo;

    @Override
    public Page<AdminDTO> getAllPage(Integer currentPage, Integer pageSize) {
        Pageable pageable = Page2ResultUtil.buildPageableByParam(currentPage, pageSize, Sort.by(Sort.Direction.DESC,"registerTime"));
        Page<Admin> adminPage = adminRepo.findByDeleted(DefaultValue.NOT_DELETED,pageable);
        return adminPage.map(AdminConvertBasic.INSTANCE::poToDto);
    }

    @Override
    public AdminDTO getOneByAdminName(String adminName) {
        Optional<Admin> adminOptional = adminRepo.findByAdminNameAndDeleted(adminName, DefaultValue.NOT_DELETED);
        return adminOptional.map(AdminConvertBasic.INSTANCE::poToDto).orElse(null);
    }

    @Override
    public AdminDTO getOneByAdminId(ObjectId adminId) {
        Optional<Admin> adminOptional = adminRepo.findByAdminIdAndDeleted(adminId,DefaultValue.NOT_DELETED);
        return adminOptional.map(AdminConvertBasic.INSTANCE::poToDto).orElse(null);
    }
}
